package com.example.modules.book;

import com.example.generator.model.Book;
import com.example.modules.user.UserDetailsVO;
import com.example.utils.Pagination;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/book")
@AllArgsConstructor
public class BookController {
    BookService bookService;

    @PostMapping
    public Pagination<BookVO> list(@RequestBody Pagination<BookVO> pagination, @AuthenticationPrincipal UserDetailsVO userInfo) {
        pagination.getParam().set("userId", userInfo.getId());
        return bookService.list(pagination);
    }

    @PostMapping("/collect")
    public void collect(Integer id, @AuthenticationPrincipal UserDetailsVO userInfo) {
        bookService.collect(id, userInfo.getId());
    }

    @PostMapping("/save")
    @PreAuthorize("hasAuthority('editBookInfo')")
    public Book save(@RequestBody Book bookVO) {
        return bookService.save(bookVO);
    }
}
